package com.stx.jeepay.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stx.jeepay.core.dao.IsvInfoDao;
import com.stx.jeepay.core.entity.IsvInfoEntity;
import com.stx.jeepay.core.entity.MchInfoEntity;
import com.stx.jeepay.core.entity.PayInterfaceConfigEntity;
import com.stx.jeepay.core.enums.CS;
import com.stx.jeepay.core.expection.BizException;
import com.stx.jeepay.core.service.IsvInfoService;
import com.stx.jeepay.core.service.MchInfoService;
import com.stx.jeepay.core.service.PayInterfaceConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("isvInfoService")
public class IsvInfoServiceImpl extends ServiceImpl<IsvInfoDao, IsvInfoEntity> implements IsvInfoService {

    @Autowired
    private MchInfoService mchInfoService;

    @Autowired
    private PayInterfaceConfigService payInterfaceConfigService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeByIsvNo(String isvNo) {
        IsvInfoEntity isvInfo = getById(isvNo);
        if (isvInfo==null){
            throw new BizException("该服务商不存在");
        }

        // 1.查询当前服务商下是否存在商户
        if (mchInfoService.count(new LambdaQueryWrapper<MchInfoEntity>()
                .eq(MchInfoEntity::getIsvNo,isvNo).eq(MchInfoEntity::getType, CS.MCH_TYPE_ISVSUB))>0){
            throw new BizException("该服务商下存在商户，不可删除");
        }

        // 2.删除当前服务商支付接口配置参数
        payInterfaceConfigService.remove(new LambdaQueryWrapper<PayInterfaceConfigEntity>()
                .eq(PayInterfaceConfigEntity::getInfoId,isvNo)
                .eq(PayInterfaceConfigEntity::getInfoType,CS.INFO_TYPE_ISV));

        // 3.删除该服务商
        boolean remove = removeById(isvNo);
        if (!remove) {
            throw new BizException("删除服务商失败");
        }
    }
}